/**
 * 
 */
package edu.cmu.mism.dgjava.data.ui.composites.option;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import edu.cmu.mism.dgjava.data.models.option.Futures;
import edu.cmu.mism.dgjava.data.models.option.OptionModelFactory;
import edu.cmu.mism.dgjava.data.ui.composites.AbstractDataComposite;
import edu.cmu.mism.dgjava.utils.SWTUtils;
import edu.cmu.mism.dgjava.views.Messages;

/**
 * @author Christian
 * 
 */
public class FuturesDataComposite extends AbstractDataComposite {
	private static final String DEFAULT_FUTURE_PRICE = "50.00";
	private static final String DEFAULT_VOLATILITY = "0.40";
	private static final String DEFAULT_RISK_FREE_RATE = "0.10";

	private Group _futureParamsGroup;
	private Label _futurePriceLbl;
	private Text _futurePriceTxt;

	private Text _volatilityTxt;
	private Label _volatilityLbl;

	private Label _riskFreeRateLbl;
	private Text _riskFreeRateTxt;

	private TableViewer _dividendViewer;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cmu.mism.dgjava.data.ui.composites.DataComposite#createControls(org
	 * .eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControls(Composite parent) {
		Composite container = new FormToolkit(parent.getDisplay()).createComposite(parent, SWT.BORDER);
		GridLayout gl = new GridLayout();
		gl.numColumns = 2;
		gl.marginHeight =0;
		gl.marginWidth = 0;
		container.setLayout(gl);

		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		container.setLayoutData(gd);
		Composite lContainer = SWTUtils.createComposite(container, 2, -1, 1);
		gd = new GridData(SWT.FILL, -1, true, false);
		gd.verticalAlignment = GridData.BEGINNING;
		lContainer.setLayoutData(gd);
		createInputParametersGroup(lContainer);
//		createDividendTableViewer(container);

//		this._dividendViewer.getTable().setEnabled(false);

		// hookup modify listener
		hookupModifyListener();

	}

	private void createInputParametersGroup(Composite container) {
		GridData hGd = new GridData(GridData.FILL_HORIZONTAL);
		GridData labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		// future price
		this._futureParamsGroup = SWTUtils.createGroup(container,
				Messages.UnderlyingDataView_group_inputParameters, 2, 2, -1,
				GridData.FILL_HORIZONTAL);
		this._futurePriceLbl = SWTUtils.createLabel(this._futureParamsGroup,
				"Futures Price", 1);
		this._futurePriceLbl.setLayoutData(labelGd);
		this._futurePriceTxt = SWTUtils.createTextBox(this._futureParamsGroup,
				DEFAULT_FUTURE_PRICE);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._futurePriceTxt.setLayoutData(hGd);

		// volatility
		this._volatilityLbl = SWTUtils.createLabel(this._futureParamsGroup,
				Messages.UnderlyingDataView_label_volatility, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		this._volatilityLbl.setLayoutData(labelGd);
		this._volatilityTxt = SWTUtils.createTextBox(this._futureParamsGroup,
				DEFAULT_VOLATILITY);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._volatilityTxt.setLayoutData(hGd);

		// risk free rate
		this._riskFreeRateLbl = SWTUtils.createLabel(this._futureParamsGroup,
				Messages.UnderlyingDataView_label_riskFreeRate, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		this._riskFreeRateLbl.setLayoutData(labelGd);
		this._riskFreeRateTxt = SWTUtils.createTextBox(this._futureParamsGroup,
				DEFAULT_RISK_FREE_RATE);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._riskFreeRateTxt.setLayoutData(hGd);

	}

	/**
	 * @param container
	 */
	private void createDividendTableViewer(Composite container) {
		Table viewTable = new Table(container, SWT.SINGLE | SWT.BORDER
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
		GridData hGd = new GridData(SWT.FILL, SWT.FILL, true, true);
		viewTable.setLayoutData(hGd);
		viewTable.setHeaderVisible(true);
		_dividendViewer = new TableViewer(viewTable);

		TableColumn timeColumn = new TableColumn(viewTable, SWT.NONE);
		timeColumn.setText("Time"); //$NON-NLS-1$
		timeColumn.setWidth(100);

		TableColumn dividend = new TableColumn(viewTable, SWT.NONE);
		dividend.setText("Dividend"); //$NON-NLS-1$
		dividend.setWidth(100);
	}

	/**
	 * Hook up modify listener to observe modification event and validate user's
	 * input
	 */
	protected void hookupModifyListener() {
		this._riskFreeRateTxt.addModifyListener(_validator);
		this._futurePriceTxt.addModifyListener(_validator);
		this._volatilityTxt.addModifyListener(_validator);

	}

	@Override
	protected void validate() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cmu.mism.dgjava.data.ui.composites.AbstractDataComposite#getDataObject
	 * ()
	 */
	@Override
	public Object getDataObject() {
		Futures future = OptionModelFactory.eINSTANCE.createFutures();
		future.setFuturesPrice(Double.parseDouble(this._futurePriceTxt
				.getText()));
		future.setRiskFreeRate(Double.parseDouble(this._riskFreeRateTxt
				.getText()));
		future.setVolatility(Double.parseDouble(this._volatilityTxt.getText()));

		return future;
	}

}
